import os

from openai import OpenAI

QUESTION_CONCLUDE_TEMPLATE = """输入为一天的聊天记录，请帮忙整理为格式化数据。只需要返回json数据即可，不要返回其他。
## 举例
返回结果：[{"id": "1", "question": "处方作废","speaker":"s.s","speak_time":"2024-12-10 11:00:00","completed": "已解决"}]
字段含义：
- id: 序号，从1开始，依次累加。
- question: 问题总结，不多于10个字来描述。
- speaker: 发言人。
- speak_time: 问题提出时间。
- completed: 是否已解决。用已解决或未解决代替
 
## 聊天记录
{wechat_dialog}
"""

os.environ['DASHSCOPE_API_KEY'] = 'sk-dd4911ae1b83470cbb87cb75ccf60794'
client = OpenAI(
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",  # 填写DashScope服务endpoint
)


def llm_request(documents):
    dialog = ''
    for document in documents:
        one_dialog = '发言人：' + document['speaker'] + '，发言内容：' + document['content'] + '，发言时间：' + document['datetime']
        dialog += one_dialog + '\n'
    prompt = QUESTION_CONCLUDE_TEMPLATE.replace("{wechat_dialog}", dialog)
    print(prompt)
    completion = client.chat.completions.create(
        model="qwen-plus",
        messages=[
            {'role': 'user', 'content': prompt}
        ],
        stream=False
    )
    print(completion)
    return completion.choices[0].message.content
